package com.ymatou.iapi.tradingsettlement.testcase;

import java.math.BigDecimal;
import java.util.List;

import org.json.JSONException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.optrade.parameter.enums.CancelOrderTypeEnum;
import com.ymatou.iapi.optrade.parameter.enums.LogisticsTypeEnum;
import com.ymatou.iapi.optrade.resp.TradingServiceResp;
import com.ymatou.iapi.synctrade.service.TradingSyncService;
import com.ymatou.iapi.tradingsettlement.parameter.ManageFeeUnSettledAmountReq;
import com.ymatou.iapi.tradingsettlement.service.ManageFeeUnSettledAmountCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.HunterCallService;
import com.ymttest.business.service.PayGateWayCallService;
import com.ymttest.business.service.YMTTrading.TradingCallService;
import com.ymttest.business.service.YMTTrading.TradingPlaceHunterOrderCallService;
import com.ymttest.database.sharding.db.sharding.model.OrderPo;
import com.ymttest.database.sharding.db.sqlwapper.OrderShardingWapper;
import com.ymttest.database.sqlwapper.hunter.HunterSettlementWrapper;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;

@Ignore
public class Ts_ManageFeeUnSettledAmount {
	
	private static ManageFeeUnSettledAmountReq manageFeeUnSettledAmountReq;
	private static ManageFeeUnSettledAmountCall manageFeeUnSettledAmountCall;
	
	private static long userId = Long.valueOf(EnvSetup.getData("BuyUserId"));
	HunterSettlementWrapper hunterSettlementWrapper = new HunterSettlementWrapper();

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("推手未结算服务费金额");
	}

	@Before
	public void caseUp() {
		manageFeeUnSettledAmountReq = new ManageFeeUnSettledAmountReq();
		manageFeeUnSettledAmountCall = new ManageFeeUnSettledAmountCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(manageFeeUnSettledAmountCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	public void verifyDb() throws JSONException {
		
		String expectUnSettledAmount = new BigDecimal(
				manageFeeUnSettledAmountCall.getData().optString(
						"unSettledAmount")).stripTrailingZeros()
				.toPlainString();
		
		String actualUnSettledAmount = hunterSettlementWrapper
				.sumManageFeeUnSettledAmonut(
						manageFeeUnSettledAmountReq.getHunterId())
				.stripTrailingZeros().toPlainString();
		
		DataUtil.verifyEquals(expectUnSettledAmount, actualUnSettledAmount, "验证unSettledAmount");
		
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ManageFeeUnSettledAmount_001() {
		Logger.start(true, "推手未结算服务费金额");
		try {
			
			Logger.comment("前置数据准备");

			OrderShardingWapper orderShardingWapper = new OrderShardingWapper();
			List<OrderPo> orderList = orderShardingWapper
					.selectHunterGiftBagOrderbyUserId(userId);

			if (orderList.size() > 0) {
				for (OrderPo po : orderList) {
					TradingCallService.cancelOrder(po.getOrderId(),
							CancelOrderTypeEnum.BUYER_CANCEL_ORDER, userId);
				}
			}

			long hunterId = 87463475000L;

			// 未结算的大礼包数据
			TradingServiceResp notSettledGiftPackagePlaceOrder = TradingPlaceHunterOrderCallService
					.placeHunterOrder(hunterId, userId, 1);

			PayGateWayCallService.notifyCall(
					"13",
					TradingCallService.payOrder(userId, BigDecimal.ZERO,
							notSettledGiftPackagePlaceOrder.getTradingId(),
							"13").toString(), notSettledGiftPackagePlaceOrder
							.getPayableAmount().subtract(BigDecimal.ZERO)
							.toString(), (int) userId);
			TradingSyncService.getSyncResult(notSettledGiftPackagePlaceOrder
					.getOrderList().get(0).getOrderId());

			// 未结算的普通商品数据
			TradingServiceResp notSettledProductPlaceOrder = TradingPlaceHunterOrderCallService
					.placeHunterOrder(hunterId, userId, 2);

			PayGateWayCallService.notifyCall(
					"13",
					TradingCallService.payOrder(userId, BigDecimal.ZERO,
							notSettledProductPlaceOrder.getTradingId(), "13")
							.toString(), notSettledProductPlaceOrder
							.getPayableAmount().subtract(BigDecimal.ZERO)
							.toString(), (int) userId);
			TradingSyncService.getSyncResult(notSettledProductPlaceOrder
					.getOrderList().get(0).getOrderId());

			// 已经结算的大礼包数据
			TradingServiceResp settledGiftPackagePlaceOrder = TradingPlaceHunterOrderCallService
					.placeHunterOrder(hunterId, userId, 1);
			long settledGiftPackagePlaceOrderSellerId = settledGiftPackagePlaceOrder
					.getOrderList().get(0).getSellerId();
			long settledGiftPackagePlaceOrderOrderId = settledGiftPackagePlaceOrder
					.getOrderList().get(0).getOrderId();

			PayGateWayCallService.notifyCall(
					"13",
					TradingCallService.payOrder(userId, BigDecimal.ZERO,
							settledGiftPackagePlaceOrder.getTradingId(), "13")
							.toString(), settledGiftPackagePlaceOrder
							.getPayableAmount().subtract(BigDecimal.ZERO)
							.toString(), (int) userId);
			TradingSyncService.getSyncResult(settledGiftPackagePlaceOrder
					.getOrderList().get(0).getOrderId());

			// 拼邮国际段发货
			TradingCallService.delivery(settledGiftPackagePlaceOrderSellerId,
					settledGiftPackagePlaceOrderOrderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 1);
			TradingSyncService
					.getSyncResult(settledGiftPackagePlaceOrderOrderId);
			// 拼邮国内段发货
			TradingCallService.delivery(settledGiftPackagePlaceOrderSellerId,
					settledGiftPackagePlaceOrderOrderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 3);

			// 推手结算
			hunterSettlementWrapper.updateExpectSettlementTimeByOrderId(
					YMTDateUtil.getBeforeOrNextDate(-1),
					settledGiftPackagePlaceOrderOrderId);
			HunterCallService.doCommissionSettlement();

			// 已经结算的普通商品数据
			TradingServiceResp settledProductPlaceOrder = TradingPlaceHunterOrderCallService
					.placeHunterOrder(hunterId, userId, 2);
			long settledProductPlaceOrderSellerId = settledGiftPackagePlaceOrder
					.getOrderList().get(0).getSellerId();
			long settledProductPlaceOrderOrderId = settledGiftPackagePlaceOrder
					.getOrderList().get(0).getOrderId();

			PayGateWayCallService.notifyCall(
					"13",
					TradingCallService.payOrder(userId, BigDecimal.ZERO,
							settledProductPlaceOrder.getTradingId(), "13")
							.toString(), settledProductPlaceOrder
							.getPayableAmount().subtract(BigDecimal.ZERO)
							.toString(), (int) userId);
			TradingSyncService.getSyncResult(settledProductPlaceOrder
					.getOrderList().get(0).getOrderId());

			// 拼邮国际段发货
			TradingCallService.delivery(settledProductPlaceOrderSellerId,
					settledProductPlaceOrderOrderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 1);
			TradingSyncService.getSyncResult(settledProductPlaceOrderOrderId);
			// 拼邮国内段发货
			TradingCallService.delivery(settledProductPlaceOrderSellerId,
					settledProductPlaceOrderOrderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 3);

			// 推手结算
			hunterSettlementWrapper.updateExpectSettlementTimeByOrderId(
					YMTDateUtil.getBeforeOrNextDate(-1),
					settledProductPlaceOrderOrderId);
			HunterCallService.doCommissionSettlement();

			Logger.comment("前置数据结束");
			
			manageFeeUnSettledAmountReq.setHunterId(74971288000L);

			manageFeeUnSettledAmountCall.setData(manageFeeUnSettledAmountReq);
			manageFeeUnSettledAmountCall.callService();
			
			verifyDb();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ManageFeeUnSettledAmount_002() {
		Logger.start(true, "推手未结算服务费金额（该推手只有佣金，没有服务费）");
		try {
			
			Logger.comment("前置数据准备");

			OrderShardingWapper orderShardingWapper = new OrderShardingWapper();
			List<OrderPo> orderList = orderShardingWapper
					.selectHunterGiftBagOrderbyUserId(userId);

			if (orderList.size() > 0) {
				for (OrderPo po : orderList) {
					TradingCallService.cancelOrder(po.getOrderId(),
							CancelOrderTypeEnum.BUYER_CANCEL_ORDER, userId);
				}
			}

			long hunterId = 1234;

			// 未结算的大礼包数据
			TradingServiceResp notSettledGiftPackagePlaceOrder = TradingPlaceHunterOrderCallService
					.placeHunterOrder(hunterId, userId, 1);

			PayGateWayCallService.notifyCall(
					"13",
					TradingCallService.payOrder(userId, BigDecimal.ZERO,
							notSettledGiftPackagePlaceOrder.getTradingId(),
							"13").toString(), notSettledGiftPackagePlaceOrder
							.getPayableAmount().subtract(BigDecimal.ZERO)
							.toString(), (int) userId);
			TradingSyncService.getSyncResult(notSettledGiftPackagePlaceOrder
					.getOrderList().get(0).getOrderId());

			// 未结算的普通商品数据
			TradingServiceResp notSettledProductPlaceOrder = TradingPlaceHunterOrderCallService
					.placeHunterOrder(hunterId, userId, 2);

			PayGateWayCallService.notifyCall(
					"13",
					TradingCallService.payOrder(userId, BigDecimal.ZERO,
							notSettledProductPlaceOrder.getTradingId(), "13")
							.toString(), notSettledProductPlaceOrder
							.getPayableAmount().subtract(BigDecimal.ZERO)
							.toString(), (int) userId);
			TradingSyncService.getSyncResult(notSettledProductPlaceOrder
					.getOrderList().get(0).getOrderId());

			// 已经结算的大礼包数据
			TradingServiceResp settledGiftPackagePlaceOrder = TradingPlaceHunterOrderCallService
					.placeHunterOrder(hunterId, userId, 1);
			long settledGiftPackagePlaceOrderSellerId = settledGiftPackagePlaceOrder
					.getOrderList().get(0).getSellerId();
			long settledGiftPackagePlaceOrderOrderId = settledGiftPackagePlaceOrder
					.getOrderList().get(0).getOrderId();

			PayGateWayCallService.notifyCall(
					"13",
					TradingCallService.payOrder(userId, BigDecimal.ZERO,
							settledGiftPackagePlaceOrder.getTradingId(), "13")
							.toString(), settledGiftPackagePlaceOrder
							.getPayableAmount().subtract(BigDecimal.ZERO)
							.toString(), (int) userId);
			TradingSyncService.getSyncResult(settledGiftPackagePlaceOrder
					.getOrderList().get(0).getOrderId());

			// 拼邮国际段发货
			TradingCallService.delivery(settledGiftPackagePlaceOrderSellerId,
					settledGiftPackagePlaceOrderOrderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 1);
			TradingSyncService
					.getSyncResult(settledGiftPackagePlaceOrderOrderId);
			// 拼邮国内段发货
			TradingCallService.delivery(settledGiftPackagePlaceOrderSellerId,
					settledGiftPackagePlaceOrderOrderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 3);

			// 推手结算
			hunterSettlementWrapper.updateExpectSettlementTimeByOrderId(
					YMTDateUtil.getBeforeOrNextDate(-1),
					settledGiftPackagePlaceOrderOrderId);
			HunterCallService.doCommissionSettlement();

			// 已经结算的普通商品数据
			TradingServiceResp settledProductPlaceOrder = TradingPlaceHunterOrderCallService
					.placeHunterOrder(hunterId, userId, 2);
			long settledProductPlaceOrderSellerId = settledGiftPackagePlaceOrder
					.getOrderList().get(0).getSellerId();
			long settledProductPlaceOrderOrderId = settledGiftPackagePlaceOrder
					.getOrderList().get(0).getOrderId();

			PayGateWayCallService.notifyCall(
					"13",
					TradingCallService.payOrder(userId, BigDecimal.ZERO,
							settledProductPlaceOrder.getTradingId(), "13")
							.toString(), settledProductPlaceOrder
							.getPayableAmount().subtract(BigDecimal.ZERO)
							.toString(), (int) userId);
			TradingSyncService.getSyncResult(settledProductPlaceOrder
					.getOrderList().get(0).getOrderId());

			// 拼邮国际段发货
			TradingCallService.delivery(settledProductPlaceOrderSellerId,
					settledProductPlaceOrderOrderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 1);
			TradingSyncService.getSyncResult(settledProductPlaceOrderOrderId);
			// 拼邮国内段发货
			TradingCallService.delivery(settledProductPlaceOrderSellerId,
					settledProductPlaceOrderOrderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 3);

			// 推手结算
			hunterSettlementWrapper.updateExpectSettlementTimeByOrderId(
					YMTDateUtil.getBeforeOrNextDate(-1),
					settledProductPlaceOrderOrderId);
			HunterCallService.doCommissionSettlement();

			Logger.comment("前置数据结束");
			
			manageFeeUnSettledAmountReq.setHunterId(hunterId);

			manageFeeUnSettledAmountCall.setData(manageFeeUnSettledAmountReq);
			manageFeeUnSettledAmountCall.callService();
			
			verifyDb();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ManageFeeUnSettledAmount_003() {
		Logger.start(true, "hunterId为0");
		try {
			
			manageFeeUnSettledAmountReq.setHunterId(0);

			manageFeeUnSettledAmountCall.setData(manageFeeUnSettledAmountReq);
			manageFeeUnSettledAmountCall.callService();
			
			DataUtil.verifyEquals(400, manageFeeUnSettledAmountCall.getJSONData()
					.getInt("code"), "验证code");
			DataUtil.verifyEquals(100, manageFeeUnSettledAmountCall.getJSONData()
					.getInt("bcode"), "验证bcode");
			DataUtil.verifyEquals("hunterId:推手id不能为空", manageFeeUnSettledAmountCall
					.getJSONData().getString("msg"), "验证msg");
			DataUtil.verifyEquals(false, manageFeeUnSettledAmountCall.getData()
					.getBoolean("success"), "验证success");
			DataUtil.verifyEquals("ILLEGAL_ARGUMENT", manageFeeUnSettledAmountCall
					.getData().getString("errorCode"), "验证errorCode");
			DataUtil.verifyEquals("hunterId:推手id不能为空", manageFeeUnSettledAmountCall
					.getData().getString("errorMessage"), "验证errorMessage");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
